home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter5 / isohex5_1 / isohex5_1.cpp next >
Encoding:
C/C++ Source or Header  |  2000-05-21  |  7.7 KB  |  319 lines

  1. /*****************************************************************************
  2. IsoHex5_1.cpp
  3. Ernest S. Pazera
  4. 21MAY2000
  5. Start a WIN32 Application Workspace, add in this file
  6. Needs ddraw.lib and dxguid.lib
  7. *****************************************************************************/
  8.  
  9. //////////////////////////////////////////////////////////////////////////////
  10. //INCLUDES
  11. //////////////////////////////////////////////////////////////////////////////
  12. #define WIN32_LEAN_AND_MEAN  
  13.  
  14. #include <windows.h>   
  15. #include "ddraw.h"
  16.  
  17. //////////////////////////////////////////////////////////////////////////////
  18. //DEFINES
  19. //////////////////////////////////////////////////////////////////////////////
  20. //name for our window class
  21. #define WINDOWCLASS "ISOHEX5"
  22. //title of the application
  23. #define WINDOWTITLE "IsoHex 5-1"
  24.  
  25. //////////////////////////////////////////////////////////////////////////////
  26. //PROTOTYPES
  27. //////////////////////////////////////////////////////////////////////////////
  28. bool Prog_Init();//game data initalizer
  29. void Prog_Loop();//main game loop
  30. void Prog_Done();//game clean up
  31.  
  32. //enumeration functions
  33. HRESULT WINAPI EnumModesCallbackCount(LPDDSURFACEDESC2 lpDDSurfaceDesc, LPVOID lpContext);
  34. HRESULT WINAPI EnumModesCallbackList(LPDDSURFACEDESC2 lpDDSurfaceDesc,LPVOID lpContext);
  35.  
  36.  
  37. //////////////////////////////////////////////////////////////////////////////
  38. //GLOBALS
  39. //////////////////////////////////////////////////////////////////////////////
  40. HINSTANCE hInstMain=NULL;//main application handle
  41. HWND hWndMain=NULL;//handle to our main window
  42. //IDirectDraw7 Pointer
  43. LPDIRECTDRAW7 lpdd=NULL;
  44. //display mode structure
  45. struct DisplayMode
  46. {
  47.     DWORD dwWidth;
  48.     DWORD dwHeight;
  49.     DWORD dwBPP;
  50. };
  51. //display mode enumeration variables
  52. DWORD dwDisplayModeCount=0;
  53. DisplayMode* DisplayModeList=NULL;
  54.  
  55. //////////////////////////////////////////////////////////////////////////////
  56. //WINDOWPROC
  57. //////////////////////////////////////////////////////////////////////////////
  58. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  59. {
  60.     //which message did we get?
  61.     switch(uMsg)
  62.     {
  63.     case WM_KEYDOWN:
  64.         {
  65.             //check for escape key
  66.             if(wParam==VK_ESCAPE)
  67.             {
  68.                 DestroyWindow(hWndMain);
  69.             }
  70.  
  71.             return(0);//handled message
  72.         }break;
  73.     case WM_DESTROY://the window is being destroyed
  74.         {
  75.  
  76.             //tell the application we are quitting
  77.             PostQuitMessage(0);
  78.  
  79.             //handled message, so return 0
  80.             return(0);
  81.  
  82.         }break;
  83.     case WM_PAINT://the window needs repainting
  84.         {
  85.             //a variable needed for painting information
  86.             PAINTSTRUCT ps;
  87.             
  88.             //start painting
  89.             HDC hdc=BeginPaint(hwnd,&ps);
  90.  
  91.             /////////////////////////////
  92.             //painting code would go here
  93.             /////////////////////////////
  94.  
  95.             //end painting
  96.             EndPaint(hwnd,&ps);
  97.                         
  98.             //handled message, so return 0
  99.             return(0);
  100.         }break;
  101.     }
  102.  
  103.     //pass along any other message to default message handler
  104.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  105. }
  106.  
  107.  
  108. //////////////////////////////////////////////////////////////////////////////
  109. //WINMAIN
  110. //////////////////////////////////////////////////////////////////////////////
  111. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  112. {
  113.     //assign instance to global variable
  114.     hInstMain=hInstance;
  115.  
  116.     //create window class
  117.     WNDCLASSEX wcx;
  118.  
  119.     //set the size of the structure
  120.     wcx.cbSize=sizeof(WNDCLASSEX);
  121.  
  122.     //class style
  123.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  124.  
  125.     //window procedure
  126.     wcx.lpfnWndProc=TheWindowProc;
  127.  
  128.     //class extra
  129.     wcx.cbClsExtra=0;
  130.  
  131.     //window extra
  132.     wcx.cbWndExtra=0;
  133.  
  134.     //application handle
  135.     wcx.hInstance=hInstMain;
  136.  
  137.     //icon
  138.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  139.  
  140.     //cursor
  141.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  142.  
  143.     //background color
  144.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  145.  
  146.     //menu
  147.     wcx.lpszMenuName=NULL;
  148.  
  149.     //class name
  150.     wcx.lpszClassName=WINDOWCLASS;
  151.  
  152.     //small icon
  153.     wcx.hIconSm=NULL;
  154.  
  155.     //register the window class, return 0 if not successful
  156.     if(!RegisterClassEx(&wcx)) return(0);
  157.  
  158.     //create main window
  159.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_POPUP | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  160.  
  161.     //error check
  162.     if(!hWndMain) return(0);
  163.  
  164.     //if program initialization failed, then return with 0
  165.     if(!Prog_Init()) return(0);
  166.  
  167.     //message structure
  168.     MSG msg;
  169.  
  170.     //message pump
  171.     for(;;)    
  172.     {
  173.         //look for a message
  174.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  175.         {
  176.             //there is a message
  177.  
  178.             //check that we arent quitting
  179.             if(msg.message==WM_QUIT) break;
  180.             
  181.             //translate message
  182.             TranslateMessage(&msg);
  183.  
  184.             //dispatch message
  185.             DispatchMessage(&msg);
  186.         }
  187.  
  188.         //run main game loop
  189.         Prog_Loop();
  190.     }
  191.     
  192.     //clean up program data
  193.     Prog_Done();
  194.  
  195.     //return the wparam from the WM_QUIT message
  196.     return(msg.wParam);
  197. }
  198.  
  199. //////////////////////////////////////////////////////////////////////////////
  200. //INITIALIZATION
  201. //////////////////////////////////////////////////////////////////////////////
  202. bool Prog_Init()
  203. {
  204.     //error code 
  205.     HRESULT hr;
  206.  
  207.     //initialize the dd pointer
  208.     hr=DirectDrawCreateEx(NULL,(void**)&lpdd,IID_IDirectDraw7,NULL);
  209.  
  210.     //example for error handling
  211.     if(FAILED(hr))
  212.     {
  213.         //initialization failed
  214.         return(false);
  215.     }
  216.  
  217.     //set the cooperative level
  218.     lpdd->SetCooperativeLevel(hWndMain,DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT);
  219.  
  220.     //enumerate the displaymodes
  221.     dwDisplayModeCount=0;
  222.  
  223.     lpdd->EnumDisplayModes(0,NULL,NULL,EnumModesCallbackCount);
  224.  
  225.     DisplayModeList=new DisplayMode[dwDisplayModeCount];
  226.     dwDisplayModeCount=0;
  227.  
  228.     lpdd->EnumDisplayModes(0,NULL,NULL,EnumModesCallbackList);
  229.  
  230.     //pick a display mode
  231.     DisplayMode TestMode;
  232.     TestMode.dwWidth=0;
  233.     TestMode.dwHeight=0;
  234.     TestMode.dwBPP=0;
  235.     DWORD index;
  236.     bool found=false;
  237.  
  238.     for(index=0;(index<dwDisplayModeCount);index++)
  239.     {
  240.         if(DisplayModeList[index].dwBPP==16)
  241.         {
  242.             if(DisplayModeList[index].dwWidth>TestMode.dwWidth)
  243.             {
  244.                 TestMode.dwWidth=DisplayModeList[index].dwWidth;
  245.                 TestMode.dwHeight=DisplayModeList[index].dwHeight;
  246.                 TestMode.dwBPP=DisplayModeList[index].dwBPP;
  247.                 found=true;
  248.             }
  249.         }
  250.     }
  251.  
  252.     if(!found)
  253.     {
  254.         return(false);
  255.     }
  256.  
  257.     //set the display mode
  258.     hr=lpdd->SetDisplayMode(TestMode.dwWidth,TestMode.dwHeight,TestMode.dwBPP,0,0);
  259.  
  260.     return(true);//return success
  261. }
  262.  
  263. //////////////////////////////////////////////////////////////////////////////
  264. //CLEANUP
  265. //////////////////////////////////////////////////////////////////////////////
  266. void Prog_Done()
  267. {
  268.     //clean up the dd pointer
  269.     if(lpdd)
  270.     {
  271.         lpdd->Release();
  272.         lpdd=NULL;
  273.     }
  274.  
  275.     //get rid of enumeration stuff
  276.     delete [] DisplayModeList;
  277. }
  278.  
  279. //////////////////////////////////////////////////////////////////////////////
  280. //MAIN GAME LOOP
  281. //////////////////////////////////////////////////////////////////////////////
  282. void Prog_Loop()
  283. {
  284.     ///////////////////////////
  285.     //main game logic goes here
  286.     ///////////////////////////
  287. }
  288.  
  289. //enumeration-count
  290. HRESULT WINAPI EnumModesCallbackCount(
  291.   LPDDSURFACEDESC2 lpDDSurfaceDesc,  
  292.   LPVOID lpContext                   
  293. )
  294. {
  295.     //increment the count variable
  296.     dwDisplayModeCount++;
  297.  
  298.     //continue the enumeration
  299.     return(DDENUMRET_OK);
  300. }
  301.  
  302. //enumeration-list
  303. HRESULT WINAPI EnumModesCallbackList(
  304.   LPDDSURFACEDESC2 lpDDSurfaceDesc,  
  305.   LPVOID lpContext                   
  306. )
  307. {
  308.     //copy applicable information to the list
  309.     DisplayModeList[dwDisplayModeCount].dwWidth=lpDDSurfaceDesc->dwWidth;
  310.     DisplayModeList[dwDisplayModeCount].dwHeight=lpDDSurfaceDesc->dwHeight;
  311.     DisplayModeList[dwDisplayModeCount].dwBPP=lpDDSurfaceDesc->ddpfPixelFormat.dwRGBBitCount;
  312.  
  313.     //increment the count variable
  314.     dwDisplayModeCount++;
  315.  
  316.     //continue the enumeration
  317.     return(DDENUMRET_OK);
  318. }
  319.